<?php

namespace Admin\Controller;

use Think\Controller;

class MenuinfoController extends Controller {

    function __construct() {
        //继承父类
        parent::__construct();

        //判断登录状态
        if (!D('Admin')->islogin()) {//未登录
            $this->error('您尚未登录，请先登录！', U('login/login'), 3);
        }
        //判断操作是否授权
        if (!D('Admin')->isoperate(CONTROLLER_NAME, ACTION_NAME)) {//未授权
            $this->error('您尚无此操作权限！', U('index/welcome'), 3);
        }
    }

    //空方法，防止报错
    public function _empty() {
        $this->index();
    }

    //菜单列表
    public function index() {
        $menu_id = I("get.menu_id", 0);
        $where = "1 and menu_fatherid=" . $menu_id . " ";
        $info = I("info");
        if ($info) {
            @extract($info);
            if ($keyword) {
                $where .= "and menu_title like '%$keyword%' ";
                $this->assign('keyword', $keyword);
            }
        }

        $menuinfo = M('Menuinfo');
        // 查询满足要求的总记录数
        $count = $menuinfo->where($where)->count();
        $Page = new \Think\Page($count, 15);
        //分页跳转的时候保证查询条件
        if ($info) {
            foreach ($info as $key => $val) {
                $Page->parameter['info[' . $key . ']'] = urlencode($val);
            }
        }
        $pageshow = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $menuinfo->where($where)->order('menu_order desc,menu_id asc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('listinfo', $listinfo);
        $this->assign('pageshow', $pageshow);

        if ($menu_id) {//非顶级菜单
            $father_menuinfo = M("Menuinfo")->where("menu_id=" . $menu_id)->find();
            $this->assign('father_menuinfo', $father_menuinfo);
        } else {//顶部菜单
            $this->assign('father_menuinfo', array("menu_id" => 0, "menu_fatherid" => 0, "menu_title" => '顶部菜单'));
        }

        $this->display();
    }

    //添加菜单
    public function addmenuinfo() {
        $menu_fatherid = I("get.menu_fatherid");
        if (IS_POST) {
            $info = I("info");
            $info['menu_title'] = trim($info['menu_title']);
            $info['menu_fatherid'] = intval($info['menu_fatherid']);
            $info['menu_type'] = intval($info['menu_type']);
            $info['menu_controller'] = strtolower(trim($info['menu_controller']));
            $info['menu_action'] = strtolower(trim($info['menu_action']));
            $info['menu_icon'] = trim($info['menu_icon']);
            $info['menu_order'] = intval($info['menu_order']);
            $flag = M("Menuinfo")->add($info);
            if ($flag) {
                $this->success('添加菜单成功！', U('menuinfo/index', array("menu_id" => $info['menu_fatherid'])), 3);
            } else {
                $this->error('添加菜单失败！', U('menuinfo/index', array("menu_id" => $info['menu_fatherid'])), 3);
            }
        } else {
            if ($menu_fatherid) {//非顶级菜单
                $father_menuinfo = M("Menuinfo")->where("menu_id=" . $menu_fatherid)->find();
                $this->assign('father_menuinfo', $father_menuinfo);
            } else {//顶部菜单
                $this->assign('father_menuinfo', array("menu_id" => 0, "menu_fatherid" => 0, "menu_title" => '顶部菜单'));
            }
            $this->display();
        }
    }

    //编辑菜单
    public function editmenuinfo() {
        $menu_id = I("get.menu_id");
        if (IS_POST) {
            $info = I("info");
            $info['menu_title'] = trim($info['menu_title']);
            $info['menu_fatherid'] = intval($info['menu_fatherid']);
            $info['menu_type'] = intval($info['menu_type']);
            $info['menu_controller'] = strtolower(trim($info['menu_controller']));
            $info['menu_action'] = strtolower(trim($info['menu_action']));
            $info['menu_icon'] = trim($info['menu_icon']);
            $info['menu_order'] = intval($info['menu_order']);
            $flag = M("Menuinfo")->where("menu_id=" . $menu_id)->save($info);
            if ($flag) {
                $this->success('编辑菜单成功！', U('menuinfo/index', array("menu_id" => $info['menu_fatherid'])), 3);
            } else {
                $this->error('编辑菜单失败！', U('menuinfo/index', array("menu_id" => $info['menu_fatherid'])), 3);
            }
        } else {
            $menuinfo = M("Menuinfo")->where("menu_id=" . $menu_id)->find();
            $this->assign('menuinfo', $menuinfo);

            if ($menuinfo['menu_fatherid']) {//非顶级菜单
                $father_menuinfo = M("Menuinfo")->where("menu_id=" . $menuinfo['menu_fatherid'])->find();
                $this->assign('father_menuinfo', $father_menuinfo);
            } else {//顶部菜单
                $this->assign('father_menuinfo', array("menu_id" => 0, "menu_fatherid" => 0, "menu_title" => '顶部菜单'));
            }
            $this->display();
        }
    }

    //删除管理员
    public function delmenuinfo() {
        $menu_id = I("get.menu_id");
        M('Menuinfo')->where('menu_id=' . $menu_id)->delete();
        $this->success('菜单删除成功！', U('menuinfo/index'), 3);
    }

}
